


set more off

***Set working directory - end users will want to change this
global workdir "E:\SRTR\HCV\replication kit\" 

set scheme s1mono 
set graphics on
graph set window fontface "Times New Roman"
graph set eps fontface Times

cd "$workdir\output\"

cap log close
log using "$workdir\logs\comparing CITS to DiD.log", text replace


****************************************
****************************************


use "$workdir\outputdata\estimation sample.dta", clear


*******
** Removals and time to TX and MELD

drop if wl_org=="KI"
drop if !inrange(can_rem_year,2005,2019)
drop if missing(can_listing_opo_id)
drop if missing(can_dgn_hep)


collapse (sum) toosickdied improved ddtx ldtx tx, by(can_dgn_hep wl_org can_listing_opo_id can_rem_year)


fillin can_dgn_hep wl_org can_listing_opo_id can_rem_year

foreach x in toosickdied improved ddtx ldtx tx {
	replace `x' = 0 if missing(`x')
}

gen year = can_rem_year

gen wl_year = can_rem_year
merge m:1 can_listing_opo_id wl_year wl_org using "$workdir/outputdata/WL composition by OPO and year.dta" // this brings in size and composition variables
drop if _merge!=3

foreach x in toosickdied improved tx ddtx ldtx {
	gen `x'_pct = `x'/numonwlhcv if can_dgn_hep==1	
	replace `x'_pct = `x'/numonwlnonhcv if can_dgn_hep==0
	gen ln`x' = ln(1+`x')
}	

drop if missing(can_listing_opo_id)

gen daa = 0
replace daa = 1 if year>=2013

gen daa_hcv = 0
replace daa_hcv = 1 if daa==1 & can_dgn_hep==1

gen timesincedaa = 0
replace timesincedaa = year - 2012 if year>2012

keep if can_dgn_hep==0

tab timesincedaa, gen(timesincedaa)

gen zero = 0

*initialize outreg file
global outregfilename cits_did_comparison
reg tx year
outreg2 using $outregfilename.xls, excel stats(coef se) keep(year) dec(4) pdec(4) replace

foreach x in tx {

	reghdfe ln`x' zero timesincedaa2-timesincedaa8 daa year, absorb(i.can_listing_opo_id##i.can_dgn_hep) cluster(i.can_listing_opo_id#i.can_dgn_hep)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) dec(4) pdec(4) append

}


******************************************************************
** Now the analogous DID

use "$workdir\outputdata\estimation sample.dta", clear

//drop if wl_org=="KI"
drop if !inrange(can_rem_year,2005,2019)
drop if missing(can_listing_opo_id)
drop if wl_org=="KI" & can_dgn_hep==1 // drops any KI candidates that were marked as HCV-related diagnosis in data prep file
replace can_dgn_hep = 1 if wl_org=="KI"
drop if missing(can_dgn_hep) // this drops LI candidates with diagnosis codes that are neither classified as hcv or non-hcv

collapse (sum) toosickdied improved ddtx ldtx tx, by(can_dgn_hep wl_org can_listing_opo_id can_rem_year)

fillin can_dgn_hep wl_org can_listing_opo_id can_rem_year

drop if _fillin==1 & wl_org=="KI" & can_dgn_hep==0 // no need to have HCV/non-HCV for KI 

* drop OPOs that never have LI activity
bysort can_listing_opo_id wl_org: egen avgfillin = mean(_fillin)
drop if avgfillin==1 & wl_org=="LI" // this drops 5 OPOs: 416, 422, 427, 460, 471

foreach x in toosickdied improved ddtx ldtx tx {
	replace `x' = 0 if missing(`x')
	gen ln`x' = ln(1+`x')
}

encode wl_org, gen(wl_org1)

** making it so that KI is retained as control group when specifying HCV vs. Non-HCV groups in regressions
gen can_dgn_nothep = 0 if wl_org=="LI" & can_dgn_hep==1
replace can_dgn_nothep = 1 if wl_org=="KI"
replace can_dgn_nothep = 1 if can_dgn_hep==0 & wl_org=="LI"

gen year = can_rem_year

gen daa = 0
replace daa = 1 if year>=2014 & wl_org=="LI"

sort can_listing_opo_id can_rem_year wl_org can_dgn_hep

foreach x in 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 {
gen treated`x' = 0
replace treated`x' = 1 if wl_org=="LI" & year==`x'
} 

rename treated2013 timesincedaa2
rename treated2014 timesincedaa3
rename treated2015 timesincedaa4
rename treated2016 timesincedaa5
rename treated2017 timesincedaa6
rename treated2018 timesincedaa7
rename treated2019 timesincedaa8

gen zero = 0

foreach x in tx {

	** Non-HCV
	reghdfe ln`x' treated2005 treated2006 treated2007 treated2008 treated2009 treated2010 treated2011 zero timesincedaa2 ///
	timesincedaa3 timesincedaa4 timesincedaa5 timesincedaa6 timesincedaa7 timesincedaa8 if can_dgn_nothep==1, absorb(can_listing_opo_id#wl_org1 year) cluster(can_listing_opo_id#wl_org1)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) dec(4) pdec(4) append

}

	
******* Now look at all transplants

use "$workdir\outputdata\estimation sample.dta", clear

drop if !inrange(can_rem_year,2005,2019)
drop if missing(can_listing_opo_id)

collapse (sum) ddtx ldtx tx, by(wl_org can_listing_opo_id can_rem_year)

fillin wl_org can_listing_opo_id can_rem_year

foreach x in ddtx ldtx tx {
	replace `x' = 0 if missing(`x')
}

bysort wl_org can_listing_opo_id: egen sumtx = sum(tx)
drop if sumtx==0 // drops the 5 OPOs with no liver TX centers
drop sumtx

foreach x in tx ddtx ldtx {
	gen ln`x' = ln(1+`x')
}	

gen year = can_rem_year

gen daa = 0
replace daa = 1 if year>=2013

gen timesincedaa = 0
replace timesincedaa = year - 2012 if year>2012

tab timesincedaa, gen(timesincedaa)

gen zero = 0

preserve 

collapse (sum) ddtx ldtx tx (firstnm) daa timesinceda* year, by(can_listing_opo_id can_rem_year)

gen zero = 0

foreach x in tx ddtx ldtx {
	gen ln`x' = ln(1+`x')
}	

foreach x in tx {

	reghdfe ln`x' zero timesincedaa2-timesincedaa8 daa year, absorb(i.can_listing_opo_id) cluster(i.can_listing_opo_id)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) dec(4) pdec(4) append

}

restore

foreach x in tx {

	reghdfe ln`x' zero timesincedaa2-timesincedaa8 daa year if wl_org=="LI", absorb(i.can_listing_opo_id) cluster(i.can_listing_opo_id)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) dec(4) pdec(4) append
	
	reghdfe ln`x' zero timesincedaa2-timesincedaa8 daa year if wl_org=="KI", absorb(i.can_listing_opo_id) cluster(i.can_listing_opo_id)
	outreg2 using $outregfilename.xls, excel stats(coef se) adds(N, e(N_clust)) dec(4) pdec(4) append

}

cap log close

